using MapsterMapper;
using AiQiuQuan.Sport.Model.Models;
using AiQiuQuan.Sport.WebApi.Entities;
using AiQiuQuan.Sport.WebApi.Repositories;
using Microsoft.EntityFrameworkCore;
using AiQiuQuan.Sport.WebApi.Core;
using AiQiuQuan.Sport.Model.Enums;

namespace AiQiuQuan.Sport.WebApi.Services.Impl
{
    /// <summary>
    /// 课程订单管理
    /// </summary>
    public class CourseOrderService : ICourseOrderService
    {
        private readonly IMapper _mapper;
        private readonly ICurrentInfo _currentInfo;
        private readonly ICourseOrderRepository _courseOrderRepository;
        private readonly ICourseRepository _courseRepository;
        private readonly IRefundOrderRepository _refundOrderRepository;

        /// <summary>
        /// ctor
        /// </summary>
        public CourseOrderService(
            IMapper mapper,
            ICourseOrderRepository courseOrderRepository,
            ICourseRepository courseRepository,
            ICurrentInfo currentInfo,
            IRefundOrderRepository refundOrderRepository)
        {
            _mapper = mapper;
            _courseOrderRepository = courseOrderRepository;
            _courseRepository = courseRepository;
            _currentInfo = currentInfo;
            _refundOrderRepository = refundOrderRepository;
        }

        /// <summary>
        /// 查询一条记录
        /// </summary>
        public async Task<CourseOrderDto> GetAsync(Guid id)
        {
            var courseOrder = await _courseOrderRepository.FirstOrDefaultAsync(m => m.ID == id);
            var result = _mapper.Map<CourseOrder, CourseOrderDto>(courseOrder);
            if (result == null)
            {
                return result;
            }

            var course = await _courseRepository.FirstOrDefaultAsync(m => m.ID == result.BusinessId);
            result.CourseState = course.State;
            result.CourseHeadUrl = course.HeadUrl;
            if (result.State == CourseOrderStateEnum.PartialRefunded || result.State == CourseOrderStateEnum.Completed)
            {
                result.RefundList = await _refundOrderRepository.Where(m => m.OrderNo == result.OrderNo).Select(m => _mapper.Map<RefundOrderDto>(m)).ToListAsync();
            }

            return result;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        public async Task<PageResult<CourseOrderDto>> PageListAsync(CourseOrderPageRequestDto request)
        {
            var queryable = from order in _courseOrderRepository.AsNoTracking()
                            join course in _courseRepository.AsNoTracking()
                            on order.BusinessId equals course.ID
                            select new CourseOrderDto
                            {
                                ID = order.ID,
                                Name = order.Name,
                                OrderNo = order.OrderNo,
                                CourseType = order.CourseType,
                                TimesCount = order.TimesCount,
                                ValidMonth = order.ValidMonth,
                                StartTime = order.StartTime,
                                EndTime = order.EndTime,
                                State = order.State,
                                Address = order.Address,
                                OrderAmount = order.OrderAmount,
                                PayAmount = order.PayAmount,
                                CreateTime = order.CreateTime,
                                CourseState = course.State,
                                UserId = order.UserId,
                                UserName = order.UserName,
                                Mobile = order.Mobile,
                            };
            if (request.IsSelf)
            {
                queryable = queryable.Where(m => m.UserId == _currentInfo.UserId);
            }

            if (request.State.HasValue)
            {
                if (request.State == CourseOrderStateEnum.PartialRefunded)
                {
                    var stateList = new List<CourseOrderStateEnum> { CourseOrderStateEnum.PartialRefunded, CourseOrderStateEnum.Completed };
                    queryable = queryable.Where(m => stateList.Contains(m.State));
                }
                else
                {
                    queryable = queryable.Where(m => m.State == request.State.Value);
                }
            }

            if (request.CourseType.HasValue)
            {
                queryable = queryable.Where(m => m.CourseType == request.CourseType.Value);
            }

            if (!string.IsNullOrEmpty(request.Name))
            {
                queryable = queryable.Where(m => m.Name.Contains(request.Name));
            }

            if (!string.IsNullOrEmpty(request.OrderNo))
            {
                queryable = queryable.Where(m => m.OrderNo.Contains(request.OrderNo));
            }

            if (!string.IsNullOrEmpty(request.Address))
            {
                queryable = queryable.Where(m => m.Address.Contains(request.Address));
            }

            if (!string.IsNullOrEmpty(request.UserName))
            {
                queryable = queryable.Where(m => m.UserName.Contains(request.UserName));
            }

            if (!string.IsNullOrEmpty(request.PhoneNo))
            {
                queryable = queryable.Where(m => m.Mobile.Contains(request.PhoneNo));
            }

            if (request.QueryDate.HasValue)
            {
                queryable = queryable.Where(m => m.CreateTime.Value.DateTime >= request.QueryDate.Value.DateTime);
            }

            var pageResult = await queryable.OrderByDescending(m => m.CreateTime).PageListAsync(request);
            return pageResult;
        }
    }
}
